<template>
  <div>
    <main-box one="商品管理" two="商品分类">
      <el-row>
        <el-button type="primary" @click="showAddcate">添加分类</el-button>
      </el-row>
      <!-- 树形控件 -->
      <table-tree
        class="tableTree"
        :data="cateList"
        show-index
        index-text="#"
        border
        :columns="columns"
        :show-row-hover="false"
        :expand-type="false"
        :selection-type="false"
      >
        <!-- 是否有效 -->
        <template slot="isok" slot-scope="scope">
          <i class="el-icon-success" v-if="scope.row.cat_deleted== false" style="color:green"></i>
          <!-- <i class="el-icon-error" v-else style="color:red"></i> -->
        </template>
        <!-- 排序 -->
        <template slot="order" slot-scope="scope">
          <el-tag v-if="scope.row.cat_level=='0'">一级</el-tag>
          <el-tag v-else-if="scope.row.cat_level=='1'" type="success">二级</el-tag>
          <el-tag v-else type="warning">三级</el-tag>
        </template>
        <!-- 操作 -->
        <template slot="opt" slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
        </template>
      </table-tree>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 30]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </main-box>

    <!-- 添加分类对话框 -->
    <el-dialog title="添加分类" @close="addCateClosed" :visible.sync="addCateDialogVisible" width="50%">
      <el-form
        :model="addcateForm"
        :rules="addcateFormRules"
        ref="addcateFormRef"
        label-width="100px"
      >
        <el-form-item label="分类名称：" prop="cat_name">
          <el-input v-model="addcateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类：">
          <!-- 父级级联选择器 -->
          <el-cascader
            expand-trigger="hover"
            v-model="selectedKeys"
            :options="parentList"
            :props="parentCascader"
            @change="parentCateChanged"
            clearable
          ></el-cascader>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="addCateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      cateList: [],
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      total: 0,
      columns: [
        {
          label: "分类名称",
          prop: "cat_name"
        },
        {
          label: "是否有效",
          prop: "cat_deleted",
          type: "template",
          template: "isok"
        },
        {
          label: "排序",
          prop: "cat_level",
          type: "template",
          template: "order"
        },
        {
          label: "操作",
          type: "template",
          template: "opt",
          width: "200px"
        }
      ],
      addCateDialogVisible: false,
      addcateForm: {
        cat_name: "",
        cat_pid: 0,
        cat_level: 0
      },
      addcateFormRules: {
        cat_name: [
          { required: true, message: "请输入分类名称", trigger: "blur" }
        ]
      },
      parentList: [],
      parentCascader: {
        expandTrigger: "hover",
        value: "cat_id",
        label: "cat_name",
        children: "children",
        checkStrictly: true
      },
      selectedKeys: []
    };
  },
  mounted() {
    this.getCateList();
  },
  methods: {
    async getCateList() {
      const { data: res } = await this.$http.get("categories", {
        params: this.queryInfo
      });
      // console.log(res);
      if (res.meta.status != 200) {
        return this.$message.error("获取数据失败");
      }
      this.cateList = res.data.result;
      this.total = res.data.total;
    },
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize;
      this.getCateList();
    },
    handleCurrentChange(newNum) {
      this.queryInfo.pagenum = newNum;
      this.getCateList();
    },
    showAddcate() {
      // 在弹出对话框之前获取父级分类的数据
      this.getParentList();
      this.addCateDialogVisible = true;
    },
    async getParentList() {
      const { data: res } = await this.$http.get("categories", {
        params: { type: 2 }
      });
      // console.log(res);
      if (res.meta.status != 200) {
        return this.$message.error("获取父级分类数据失败");
      }
      this.parentList = res.data;
      // console.log(res.data);
    },
    parentCateChanged() {
      // console.log(this.selectedKeys);

      //length>0,说明选中了父级分类
      if (this.selectedKeys.length > 0) {
        // 父级分类ID
        this.addcateForm.cat_pid = this.selectedKeys[
          this.selectedKeys.length - 1
        ];
        // 当前分类等级赋值
        this.addcateForm.cat_level = this.selectedKeys.length;
      } else {
        this.addcateForm.cat_pid = 0;
        this.addcateForm.cat_level = 0;
      }
    },
    addCate() {
      // console.log(this.addcateForm);
      this.$refs.addcateFormRef.validate(async v => {
        if (v) {
          const { data: res } = await this.$http.post(
            "categories",
            this.addcateForm
          );
          // console.log(res);
          if (res.meta.status != 201) {
            return this.$message.error("添加分类失败");
          }
          this.$message.success("添加分类成功");
          this.getCateList();
          this.addCateDialogVisible = false;
        }
      });
    },
    addCateClosed() {
      this.$refs.addcateFormRef.resetFields();
      this.addcateForm.cat_pid = 0;
      this.addcateForm.cat_level = 0;
      this.addcateForm.cat_name = "";
    },
  }
};
</script>
<style lang="less" scoped>
.tableTree {
  margin-top: 15px;
}
.el-cascader {
  width: 100%;
}
</style>